﻿/*  CamHelper (See http://camhelper.codeplex.com/ or https://bitbucket.org/AndiKS/camhelper for more Info)
    Copyright (C) 2011 Andreas Wimmer

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.IO;
using System.Windows.Forms;
using CamHelper.Engine;
using CamHelper.Facade.TopSolid.Interfaces;
using Ninject;
using Ninject.MockingKernel.RhinoMock;
using NUnit.Framework;
using Rhino.Mocks;
using SystemWrapper.IO;

namespace CamHelper.Tests.Engine
{
    [TestFixture]
    public class TopSolidApplicationTest
    {

        #region IsDocumentOpen

        [Test]
        public void IsDocumentOpen_0()
        {
            var comappMock = MockRepository.GenerateMock<ITSApplication>();
            comappMock.Expect(x => x.Documents.Count).Return(0);

            var kernel = new RhinoMocksMockingKernel();
            kernel.Bind<ITSApplication>().ToConstant(comappMock);
            var app = kernel.Get<TopSolidApplication>();
            var docopen = app.IsDocumentOpen();

            comappMock.VerifyAllExpectations();
            Assert.That(docopen, Is.False);
        }

        [Test]
        public void IsDocumentOpen_1()
        {
            var comappMock = MockRepository.GenerateMock<ITSApplication>();
            comappMock.Expect(x => x.Documents.Count).Return(1);

            var kernel = new RhinoMocksMockingKernel();
            kernel.Bind<ITSApplication>().ToConstant(comappMock);
            var app = kernel.Get<TopSolidApplication>();
            var docopen = app.IsDocumentOpen();

            comappMock.VerifyAllExpectations();
            Assert.That(docopen, Is.True);
        }

        [Test]
        public void IsDocumentOpen_9()
        {
            var comappMock = MockRepository.GenerateMock<ITSApplication>();
            comappMock.Expect(x => x.Documents.Count).Return(9);

            var kernel = new RhinoMocksMockingKernel();
            kernel.Bind<ITSApplication>().ToConstant(comappMock);
            var app = kernel.Get<TopSolidApplication>();
            var docopen = app.IsDocumentOpen();

            comappMock.VerifyAllExpectations();
            Assert.That(docopen, Is.True);
        }

        #endregion

        #region GetCurrentDocument

        [Test]
        public void GetCurrentDocument()
        {
            var comappMock = MockRepository.GenerateMock<ITSApplication>();
            var comdocMock = MockRepository.GenerateMock<ITSDocument>();
            comappMock.Expect(x => x.CurrentDocument).Return(comdocMock);

            var kernel = new RhinoMocksMockingKernel();
            kernel.Bind<ITSApplication>().ToConstant(comappMock);
            var app = kernel.Get<TopSolidApplication>();
            var docretun = app.GetCurrentDocument();

            comappMock.VerifyAllExpectations();
            Assert.That(docretun.ComDocument == comdocMock);
        }

        [Test]
        [ExpectedException(typeof(CurrentDocumentNotFoundException))]
        public void GetCurrentDocument_Exception()
        {
            var comappMock = MockRepository.GenerateMock<ITSApplication>();
            var comdocMock = MockRepository.GenerateMock<ITSDocument>();
            comappMock.Expect(x => x.CurrentDocument).Throw(new Exception());

            var kernel = new RhinoMocksMockingKernel();
            kernel.Bind<ITSApplication>().ToConstant(comappMock);
            var app = kernel.Get<TopSolidApplication>();
            var docretun = app.GetCurrentDocument();

            comappMock.VerifyAllExpectations();
            Assert.That(docretun.ComDocument == comdocMock);
        }

        #endregion

        #region Message

        [Test]
        [ExpectedException(typeof(TopSolidMacroNotFoundException))]
        public void Message_MacroNotFound()
        {
            string msg = "Hello World!";
            string path = Path.Combine(Application.StartupPath, "Macro\\Message.lob");
            var comappMock = MockRepository.GenerateMock<ITSApplication>();
            comappMock.Expect(x => x.PushStringOnStack(msg));
            comappMock.Expect(x => x.ExecuteMacroKeepStack(path));

            var kernel = new RhinoMocksMockingKernel();
            kernel.Bind<ITSApplication>().ToConstant(comappMock);
            var app = kernel.Get<TopSolidApplication>();
            app.Message(msg);

            comappMock.VerifyAllExpectations();
        }

        [Test]
        public void Message()
        {
            string msg = "Hello World!";
            string path = Path.Combine(Application.StartupPath, "Macro\\Message.lob");
            var comappMock = MockRepository.GenerateMock<ITSApplication>();
            comappMock.Expect(x => x.PushStringOnStack(msg));
            comappMock.Expect(x => x.ExecuteMacroKeepStack(path));
            var fileMock = MockRepository.GenerateMock<IFileWrap>();
            fileMock.Expect(x => x.Exists(path)).Return(true);

            var kernel = new RhinoMocksMockingKernel();
            kernel.Bind<ITSApplication>().ToConstant(comappMock);
            kernel.Bind<IFileWrap>().ToConstant(fileMock);
            var app = kernel.Get<TopSolidApplication>();
            app.Message(msg);

            comappMock.VerifyAllExpectations();
        }

        #endregion

    }
}
